# coding: utf-8
from django.http import HttpResponseRedirect, Http404
from google.appengine.api import users
from pycode import render_to
from pycode.account import get_current_user_profile, search_user_profile
import logging

@render_to("account/profile.html")
def profile(request):
	user = get_current_user_profile()
	if request.POST:
		# TODO: Добавить тут всякие проверки на пустоту и занятость ников
		user.nick = request.POST.get("nick")
		user.summary = request.POST.get("summary")
		user.lang = request.POST.get("lang")
		user.hide_email = (request.POST.get("hide_email") == "on")
		user.put()
	return { "user": user, "user_is_current": True }

@render_to("account/profile.html")
def other_profile(request, name):
	return { "user": search_user_profile(name) }

def logout(request):
	if users.get_current_user():
		return HttpResponseRedirect(users.create_logout_url("/"))
	raise Http404

def login(request):
	if not users.get_current_user():
		return HttpResponseRedirect(users.create_login_url("/"))
	raise Http404
